﻿using Newtonsoft.Json;
using RulesEngine.Actions;
using RulesEngine.Models;
using System.Text;
using 小明喝汽水;

var json = File.ReadAllText("Rule.json", Encoding.UTF8);

var workflow = JsonConvert.DeserializeObject<Workflow>(json);
foreach (var rule in workflow.Rules.ToList())
{
    rule.PreProcess(workflow);
}

RulesEngine.RulesEngine rulesEngine = null;

var reSettings = new ReSettings
{
    CustomTypes = [typeof(Utils)],
    CustomActions = new Dictionary<string, Func<ActionBase>>
    {
        { "InvokeRule", () => new InvokeRuleAction(rulesEngine) }
    }
};

rulesEngine = new RulesEngine.RulesEngine(new Workflow[] { workflow }, reSettings);

var ruleResults = await rulesEngine.ExecuteAllRulesAsync("小明喝汽水", new RuleParameter("x", new
{
    Money = 20
}));

Console.WriteLine($"小明共喝了 {ruleResults.First().ActionResult.Output} 瓶汽水");
Console.ReadLine();